//
//  WBEmoticonTool.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/20.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBEmoticonTool.h"
#import "WBEmoticonModel.h"



@interface  WBEmoticonTool()



//默认表情组 一维数组
@property (nonatomic , strong) NSArray<WBEmoticonModel *> *defaultEmoticons;

//emoji表情
@property (nonatomic , strong) NSArray<WBEmoticonModel *> *emojiEmoticons;

//浪小花表情
@property (nonatomic , strong) NSArray<WBEmoticonModel *> *lxhEmoticons;





@end

@implementation WBEmoticonTool

- (NSBundle *)emoticonBundle
{
    if(!_emoticonBundle)
    {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Emoticons.bundle" ofType:nil];
        
        _emoticonBundle = [[NSBundle alloc] initWithPath:path];
    
    }
    
    return _emoticonBundle;

}

- (NSArray<WBEmoticonModel *> *)defaultEmoticons
{
    if(!_defaultEmoticons)
    {
        _defaultEmoticons = [self getEmoticonsWithFile:@"default"];
    
    }
    
    return _defaultEmoticons;

}

- (NSArray<WBEmoticonModel *> *)emojiEmoticons
{
    if(!_emojiEmoticons)
    {
        
        _emojiEmoticons = [self getEmoticonsWithFile:@"emoji"];
    
    }
    
    return _emojiEmoticons;

}



- (NSArray<WBEmoticonModel *> *)lxhEmoticons
{
    if(!_lxhEmoticons)
    {
        _lxhEmoticons = [self getEmoticonsWithFile:@"lxh"];
    }
    
    return _lxhEmoticons;

}

- (NSArray<NSArray<NSArray<WBEmoticonModel *> *> *> *)allEmoticons
{
    return @[
             [self getEmoticonsGroups:self.defaultEmoticons],
             [self getEmoticonsGroups:self.emojiEmoticons],
             [self getEmoticonsGroups:self.lxhEmoticons]
             
             ];

}


#pragma mark - 单例实现
static WBEmoticonTool *instance;
+ (instancetype)sharedEmoticonTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[WBEmoticonTool alloc] init];
    });
    
    return instance;

}

//根据文件名字 创建一维数组
- (NSArray<WBEmoticonModel *> *)getEmoticonsWithFile:(NSString *)fileName
{
    NSArray *arr = [NSArray arrayWithContentsOfFile:[self.emoticonBundle pathForResource:[NSString stringWithFormat:@"%@/info.plist",fileName] ofType:nil]];

    //创建临时可变数组
    NSMutableArray *arrM = [NSMutableArray array];
    
    //数组里面存储的是字典 遍历数组 字典转模型
    for (NSDictionary *dict in arr) {
        
        //创建模型
        WBEmoticonModel *model = [WBEmoticonModel emoticonModelWithDictionary:dict];
        model.path = fileName;
        
        //添加到临时可变数组中
        [arrM addObject:model];
    }
    
    return arrM.copy;
}

//根据一维数组 截取为二维数组
- (NSArray<NSArray<WBEmoticonModel *> *> *)getEmoticonsGroups:(NSArray *)emoticons
{
    
    //计算这个一位数组 可以 分为几页
    int page = ((int)emoticons.count + WBEmoticonsMaxCount - 1) / WBEmoticonsMaxCount;
    
    //创建临时可变数组
    NSMutableArray *tempArrM = [NSMutableArray array];
    
    //遍历页数 截取数组
    for(int i = 0 ; i < page ; i++)
    {
        
        int loc = i * WBEmoticonsMaxCount;
        
        int len = WBEmoticonsMaxCount;
        
        if(len + loc > emoticons.count)
        {
            len = (int)emoticons.count - loc;
            
        }
        
        NSRange range = NSMakeRange(loc, len);
        
        NSArray *arr = [emoticons subarrayWithRange:range];
        
        //添加到可变数组中
        [tempArrM addObject:arr];
        
    }
    
    return tempArrM.copy;

}












@end
